package com.kye.express.business.order.remark;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.beloo.widget.chipslayoutmanager.SpacingItemDecoration;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityRemarkBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.express.view.reclyerview.OnItemClickListener;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.drakeet.multitype.MultiTypeAdapter;

public class RemarkActivity extends BaseAppCompatActivity implements RemarkContract.RemarkView {
    public static final int REMARK_ACTIVITY_REQUEST_CODE = 2;

    public static final String KEY_REMARK = "KEY_REMARK";

    private ActivityRemarkBinding mBinding;
    private RemarkContract.RemarkPresenter mPresenter;
    private MultiTypeAdapter mAdapter;
    private RemarkViewBinder mBinder;

    public static void startActivityForResult(Activity activity, String remark, int requestCode) {
        activity.startActivityForResult(new Intent(activity, RemarkActivity.class).putExtra(KEY_REMARK, remark), requestCode);
    }

    public static void startActivityForResult(Activity activity, String remark) {
        startActivityForResult(activity, remark, REMARK_ACTIVITY_REQUEST_CODE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_remark);

        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mBinding.recyclerViewRemarks.setLayoutManager(ChipsLayoutManager.newBuilder(this)
                .setOrientation(ChipsLayoutManager.HORIZONTAL)
                .build());
        int spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
        mBinding.recyclerViewRemarks.addItemDecoration(new SpacingItemDecoration(spacing, spacing));
        mAdapter = new MultiTypeAdapter();
        mBinder = new RemarkViewBinder();
        mAdapter.register(String.class, mBinder);
        mAdapter.setHasStableIds(true);
        mBinding.recyclerViewRemarks.setAdapter(mAdapter);
    }

    private void initData() {
        mBinding.editTextRemark.setText(getIntent().getStringExtra(KEY_REMARK));

        mPresenter = new RemarkContract.RemarkPresenter();
        mPresenter.bind(this);

        mPresenter.getRemarks();
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        mBinder.setOnItemClickListener(new OnItemClickListener<String>() {
            @Override
            public void onClickItem(String item, View view) {
                mBinding.editTextRemark.setText(item);
            }
        });

        RxTextView.textChanges(mBinding.editTextRemark)
                .map(new Function<CharSequence, Boolean>() {
                    @Override
                    public Boolean apply(CharSequence charSequence) throws Exception {
                        return !TextUtils.isEmpty(charSequence);
                    }
                })
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        mBinding.buttonFinish.setEnabled(aBoolean);
                    }
                });

        RxView.clicks(mBinding.buttonFinish)
                .map(new Function<Object, String>() {
                    @Override
                    public String apply(Object o) throws Exception {
                        return mBinding.editTextRemark.getText().toString().trim();
                    }
                })
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        mPresenter.saveRemark(s);
                    }
                });
    }

    @Override
    public void onRemarks(List<String> strings) {
        mAdapter.setItems(strings);
    }

    @Override
    public void onFinish(String remark) {
        setResult(RESULT_OK, new Intent().putExtra(KEY_REMARK, remark));
        finish();
    }
}
